using System;
using System.Collections.Generic;
using System.Linq;
using TeamCityMonitor.Manager.CustomCreators;
using TeamCityMonitor.Models;
using TeamCityMonitor.Repository;
using TeamCityMonitor.Repository.Caches;

namespace TeamCityMonitor.Manager
{
    public interface ICustomManager : IManager
    {
        List<CustomModel> GetCustomModels();
    }

    public class CustomManager : ICustomManager
    {
        private readonly ITeamCityRepository _teamCityRepository;
        private IEnumerable<ICustomModelCreator> buildCreators;

        public CustomManager()
        {
            _teamCityRepository = new TeamCityRepository(new UserPictureRepository());

            buildCreators = new List<ICustomModelCreator>
                                {
                                    new StatisticsCreator(_teamCityRepository),
                                    new HeroCreator(_teamCityRepository, new HeroStoryRepository(), new UserPictureRepository()),
                                    new QuoteCreator(new QuoteRepository()),
                                    new NotificationCreator(new NotificationRepository()),
                                };
        }

        public List<CustomModel> GetCustomModels()
        {
            return GenerateRandomPositiveInformation();
        }

        public int GetNoOfFeedsToShowAtOnce()
        {
            return 1;
        }

        public int GetFeedDelay()
        {
            return Properties.Settings.Default.FeedDelayWhenAllIsGood;
        }

        private List<CustomModel> GenerateRandomPositiveInformation()
        {
            var builds = new List<CustomModel>();
            buildCreators.Where(creator => creator.IsActive())
                                    .ToList().ForEach(creator => builds.AddRange(creator.GetCustomModels()));

            return builds.OrderBy(build => Guid.NewGuid()).ToList();
        }
    }
}